function dumpplot(fname,mode,trange,tick1,yrange)
% DUMPPLOT    Plot tcpdump files.
%    DUMPPLOT(FILENAME) plots the specified tcpdump file with SEQno and
%    ACKno shown on it. The default column number for SEQno and ACKno in
%    tcpdump file is 6 and 11 respectively.
%
%    DUMPPLOT(FILENAME,TRANGE) plots the specified tcpdump file with
%    time range TRANGE.
%
%    DUMPPLOT(FILENAME,TRANGE,YRANGE) plots the specified tcpdump file with
%    time range TRANGE and Y value range YRANGE.
%
%    Example:
%       dumpplot('test.tcpdump.out');
%       dumpplot('test.tcpdump.out', 90000);
%       dumpplot('test.tcpdump.out', 90000, [591 593]);
%       dumpplot('test.tcpdump.out', 90000, [591 593], [0.01 1.01]);

if (nargin < 1) | (nargin > 5)
   error('Wrong number of input arguments. Try help dumpplot.');
end

if nargin > 1
  modunit = mode;
else
  modunit = 90000;
end

if nargin > 2
   stime = trange(1);
   etime = trange(2);
else
   stime = -1;
end

if nargin > 4
   miny = yrange(1);
   maxy = yrange(2);
else
   miny = -1;
end

ldcmd = sprintf('load %s;', fname);
eval(ldcmd);
fnamehd = strtok(fname, '.');
asgncmd = sprintf('D = %s;', fnamehd);
eval(asgncmd);
clrcmd = sprintf('%s = [];', fnamehd);
eval(clrcmd);

[row col] = size(D);
if col < 11
   error('Not enough columns in tcpdump file.');
end

S = [];
numS = 0;
A = [];
numA = 0;

B = [];
numB = 0;
C = [];
numC = 0;

SA = [];
numSA = 0;
SYN = [];
numSYN = 0;

PG = [];
numPG = 0;
PGD = [];
numPGD = 0;


if stime < 0
   for i = 1:row
      if (D(i,8) == 0) & (D(i,9) == 0) & (D(i,10) == 1)
        if D(i,7) == -1
          if D(i,11) >= 0 %dropped ACK
            numB = numB + 1;
            B(numB,:) = [D(i,1) rem(D(i,11),modunit)];
	  else  %dropped piggy packet
	    numPGD = numPGD + 1;
            PGD(numPGD,:) = [D(i,1) rem(D(i,6),modunit)];
	  end
        else
	  if D(i,7) == 0 % pure ACK
            numA = numA + 1;
            A(numA,:) = [D(i,1) rem(D(i,11),modunit)];
	  else % piggy packet
            numPG = numPG + 1;
            PG(numPG,:) = [D(i,1) rem(D(i,6),modunit)];
          end
        end
      else 
	if (D(i,8) == 1) & (D(i,9) == 0) 
	  if (D(i,10) == 1)
	    numSA = numSA + 1;
            SA(numSA,:) = [D(i,1) rem(D(i,6),modunit)];
          else
            numSYN = numSYN + 1;
            SYN(numSYN,:) = [D(i,1) rem(D(i,6),modunit)];
	  end
        else
          if D(i,7) == -1
            numC = numC + 1;
            C(numC,:) = [D(i,1) rem(D(i,6),modunit)];
          else
            numS = numS + 1;
            S(numS,:) = [D(i,1) rem(D(i,6),modunit)];
          end
        end
      end
   end
else
   for i = 1:row
      if (stime <= D(i,1)) & (D(i,1) <= etime)
         if (D(i,8) == 0) & (D(i,9) == 0) & (D(i,10) == 1)
            if D(i,7) == -1
              if D(i,11) >= 0 %dropped ACK
                numB = numB + 1;
                B(numB,:) = [D(i,1) rem(D(i,11),modunit)];
              else  %dropped piggy packet
                numPGD = numPGD + 1;
                PGD(numPGD,:) = [D(i,1) rem(D(i,6),modunit)];
              end
            else
              if D(i,7) == 0 % pure ACK
                numA = numA + 1;
                A(numA,:) = [D(i,1) rem(D(i,11),modunit)];
              else % piggy packet
                numPG = numPG + 1;
                PG(numPG,:) = [D(i,1) rem(D(i,6),modunit)];
              end
            end
         else 
	   if (D(i,8) == 1) & (D(i,9) == 0)
             if (D(i,10) == 1)
               numSA = numSA + 1;
               SA(numSA,:) = [D(i,1) rem(D(i,6),modunit)];
             else
               numSYN = numSYN + 1;
               SYN(numSYN,:) = [D(i,1) rem(D(i,6),modunit)];
             end
           else
             if D(i,7) == -1
               numC = numC + 1;
               C(numC,:) = [D(i,1) rem(D(i,6),modunit)];
             else
               numS = numS + 1;
               S(numS,:) = [D(i,1) rem(D(i,6),modunit)];
             end
           end
         end
      end
   end
end
set(gcf,'Units','pixels');
set(gcf,'Position',[20 40 800 600]);

splot = 0;
if ~isempty(S)
   plot(S(:,1),S(:,2),'ob','MarkerSize',4);
   splot = 1;
end
hold on;
aplot = 0;
if ~isempty(A)
   plot(A(:,1),A(:,2),'^r','MarkerSize',2);
   aplot = 1;
end
cplot = 0;
if ~isempty(C)
   plot(C(:,1),C(:,2),'xm','MarkerSize',6);
   cplot = 1;
end
bplot = 0;
if ~isempty(B)
   plot(B(:,1),B(:,2),'xr','MarkerSize',6);
   bplot = 1;
end
SYNplot = 0;
if ~isempty(SYN)
   plot(SYN(:,1),SYN(:,2),'db','MarkerSize',6);
   SYNplot = 1;
end
SAplot = 0;
if ~isempty(SA)
   plot(SA(:,1),SA(:,2),'dr','MarkerSize',6);
   SAplot = 1;
end
PGplot = 0;
if ~isempty(PG)
   plot(PG(:,1),PG(:,2),'sb','MarkerSize',6);
   PGplot = 1;
end
PGDplot = 0;
if ~isempty(PGD)
   plot(PGD(:,1),PGD(:,2),'sr','MarkerSize',6);
   PGDplot = 1;
end

if nargin > 3
  tick2 = etime;
  z = 0;
  cmd = sprintf('tick = [');
  for i=tick1: 0.2 : tick2 
    z = z+1;
    str = sprintf(' %d',i);
    cmd = strcat(cmd,str);
    if mod(z,2) == 1
      lab = sprintf('%8.2f| | ',i);
      label = strcat(label,lab);
    end
  end
  cmd = strcat(cmd,'];');
  eval(cmd);
  set(gca,'XTick',tick);
  set(gca,'XTickLabel',label);
end


xlabel('Time (seconds)');
ylabcmd = sprintf('ylabel(''Number (bytes mod %d)'');', modunit);
eval(ylabcmd);

if splot == 1
   lgndcmd = '''SEQno'', ';
else
   lgndcmd = '';
end
if aplot == 1
   lgndcmd = strcat(lgndcmd, '''ACKno'', ');
end
if cplot == 1
   lgndcmd = strcat(lgndcmd, '''dropped SEQno'', ');
end
if bplot == 1
   lgndcmd = strcat(lgndcmd, '''dropped ACKno'', ');
end
if SYNplot == 1
   lgndcmd = strcat(lgndcmd, '''SYN SEQno'', ');
end
if SAplot == 1
   lgndcmd = strcat(lgndcmd, '''SYN&ACK SEQno'', ');
end
if PGplot == 1
   lgndcmd = strcat(lgndcmd, '''Piggy Packet SEQno'', ');
end
if PGDplot == 1
   lgndcmd = strcat(lgndcmd, '''dropped Piggy Packet SEQno'', ');
end


if ~isempty(lgndcmd)
   lgndcmd = sprintf('legend(%s 2);', lgndcmd);
   eval(lgndcmd);
end
hold off;

if stime >= 0
   set(gca, 'XLim', [stime etime]);
end

if miny >= 0
   set(gca, 'YLim', [miny maxy]);
else
   set(gca, 'YLim', [0 modunit]);
end

fn = strrep(fname,'_','\_');
titcmd = sprintf('title(''%s'');', fn);
eval(titcmd);
box on;
grid on;

[I,map] = capture;
imcmd = sprintf('imwrite(I,map,''%s.jpg'',''jpeg'');', fnamehd);
eval(imcmd);

set(gcf,'PaperUnits','points');
set(gcf,'PaperPosition',[20 20 800 600]);
print(gcf, '-depsc', fnamehd);
